package com.ask.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ask.dto.LoginFormDTO;
import com.ask.dto.Result;
import com.ask.dto.UserDTO;
import com.ask.entity.User;
import com.ask.entity.UserInfo;
import com.ask.service.businessService.IUserInfoService;
import com.ask.service.businessService.IUserService;
import com.ask.threadlocals.UserHolder;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserInfoService userInfoService;

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        if (user == null) return Result.success();
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
        return Result.success(userDTO);
    }

    /**
     * 发送验证码
     *
     * @param phone
     * @param session
     * @return
     */
    @PostMapping("/code")
    public Result sendCode(String phone, HttpSession session) {
        return userService.sendCode2Redis(phone);
        // return userService.sendCode2Session(phone, session);
    }

    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginForm) {
        return userService.loginByRedis(loginForm);
        //return userService.loginBySession(phone, code, session);
    }

    /**
     * 返回当前登录用户的信息
     * @return
     */
    @GetMapping("/me")
    public Result me() {
        UserDTO user = UserHolder.getUser();
        return  Result.success(user);
    }


    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long userId) {
        UserInfo userInfo = userInfoService.getById(userId);
        if (userInfo == null)
            return Result.success();
        userInfo.setCreateTime(null);
        userInfo.setUpdateTime(null);
        return Result.success(userInfo);
    }

    /**
     * 用户登录
     * @return
     */
    @PostMapping("/sign")
    public Result sign() {
        return userService.sign();
    }

    /**
     * 统计连续签到
     * @return
     */
    @PostMapping("/sign/count")
    public Result signCount() {
        return userService.signCount();
    }

    /* ----------------------测试接口---------------------- */
    @PostMapping("/adduser")
    public Result addUser(@RequestBody UserDTO userDTO) {
        log.info("新增用户，" + userDTO );
        User user = BeanUtil.copyProperties(userDTO, User.class);
        user.setPhone("1" + (new Random().nextInt(9000000)));
        userService.save(user);
        return Result.success();
    }


    /**
     * 跟新用户
     * @param userDTO
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody UserDTO userDTO) {
        User user = BeanUtil.copyProperties(userDTO, User.class);
        userService.updateById(user);
        return Result.success();
    }


}
